function init(){
	var currentValue;// 定义即将修改的字段的原始数据
	var rows = 10;
	var page = 0;
	$('.new-btn').click(function(){
		$('#titleLab').text('新增仓库');
	    $('.add-warehouse').removeClass('hide');
	});
	$('.quxiao').click(function(){
	    $('.add-warehouse').addClass('hide');
	});
	
	/**
	 * 新增仓库
	 */
	$('.queren').click(function(){
		$.ajax({
			type:"POST",
			async:false,
			url:"/diconet/warehouse/warehouse/saveOrUpdate",
			data:$('#warehouseForm').serialize(),
			dataType : 'text', 
			success:function(result){
				var data = eval("("+result+")");
				if(data.success=="T"){
					window.location.reload();
					layer.msg('保存成功', {time:1000,icon:2}); 
				}else{
					var json = data.message;
					$.each(json, function(key) {
						$("#"+key).next().text(json[key]);
					});
				}
			},
			error:function(result){
				layer.msg('请求失败', {time:1000,icon:5}); 
			}
		});
	});
	/**
	 * 仓库名称查询的方法
	 */
	$('.search-box').on('click','span',function(){
		var warehouseName = $('#searchWarehouseName').val();
		loadComment(page, rows, warehouseName);
	});
	
	/**
	 * 打开数据修改方法
	 */
	$(".warehouse-list-tbody").on('dblclick','.table-td-input',function(){
		// 双击之后打开数据修改的输入框
		$("input[class='table-td-input input-active']").removeClass('input-active');
		$(this).addClass('input-active').removeAttr("readonly");
		currentValue = $(this).context.value;// 点击的时候记录当前值，修改之前判断值是否更改
	});
	
	loadComment(page, rows, '');
}

/**
 * 修改仓库
 * @param id
 */
function revise(id){
	$.ajax({
		type:"POST",
		async:false,
		url:"/diconet/warehouse/warehouse/findOne",
		data:{id:id},
		dataType : 'text', 
		success:function(result){
			var data = eval('(' + result + ')').data;
			if(data){
				$('#id').val(data.id);
				$('#warehouseName').val(data.warehouseName);
				$('#distributorId').val(data.distributorId);
				$('#manager').val(data.manager);
				$('#tel').val(data.tel);
				$('#cityId').val(data.cityId);
				$('#area').val(data.area);
				$('#usedPercent').val(data.usedPercent);
				$('#address').val(data.address);
				$('#remark').val(data.remark);
				$('#titleLab').text('修改仓库');
				$('.add-warehouse').removeClass('hide');
			}else{
				layer.msg('数据不存在', {time:1000,icon:5}); 
			}
		},
		error:function(result){
			layer.msg('请求失败', {time:1000,icon:5}); 
		}
	});
}

/**
 * 删除
 */
function remove(id){
	layer.confirm('您确认想要删除记录吗？', {
		btn: ['删除','取消'] //按钮
	}, function(){
		$.ajax({
			type:"POST",
			async:false,
			url:"/diconet/warehouse/warehouse/remove",
			data:{id:id},
			dataType : 'text', 
			success:function(result){
				var data = eval("("+result+")");
				if(data.success=="T"){
					window.location.reload();
				}else{
					layer.msg('删除失败', {time:1000,icon:2}); 
				}
			},
			error:function(result){
				layer.msg('请求失败', {time:1000,icon:5}); 
			}
		});
	}, function(){});
}

/**
 * 加载数据
 * @param page
 * @param rows
 */
function loadComment(page, rows, warehouseName) {
	row = page;
	$.ajax({
		type: "post",
		url: '/diconet/warehouse/warehouse/list/page',
		data: {
			page : page,
			rows : rows,
			warehouseName: warehouseName
		},
		dataType: 'json',
		success: function(data){
			var commentContent = '';
			if(data) {
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					commentContent += '<tr>';
					commentContent += '<td class="t1">'+comment.warehouseName+'</td>';
					commentContent += '<td class="t1">'+comment.area+'</td>';
					commentContent += '<td class="t1">'+comment.address+'</td>';
					commentContent += '<td class="t1">'+comment.manager+'</td>';
					commentContent += '<td class="t1">'+comment.remark+'</td>';
					// <button class="revise" onclick="revise(\''+comment.id+'\')">修改</button>
					commentContent += '<td><button class="revise" onclick="revise(\''+comment.id+'\')">修改</button><button class="delete" onclick="remove(\''+comment.id+'\')">删除</button></td>';
					commentContent += '</tr>';
				}
				$('.warehouse-list-tbody').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+','+warehouseName+');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+','+warehouseName+');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+','+warehouseName+');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+','+warehouseName+');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+','+warehouseName+');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+','+warehouseName+');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+','+warehouseName+');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+','+warehouseName+');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:1000,icon:5}); 
		}
	});
}
init();